<?php

namespace app\admin\controller;

use app\common\model\AdminModel;
use app\common\model\WhiteIpModel;
use think\facade\Request;

class WhiteIpController extends AdminController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        return view("index");
    }

    public function lists()
    {
        $where = [];

        $all = $this->getAllParams();
        if (isset($all["ip"]) && $all["ip"]) {
            array_push($where, ["ip", "=", $all["ip"]]);
        }
        if (isset($all["admin_name"]) && $all["admin_name"]) {
            $search_admin_ids = AdminModel::where("name", "LIKE", $all["admin_name"] . "%")->column("id");
            array_push($where, ["admin_id", "IN", $search_admin_ids]);
        }
        if (isset($all["status"]) && $all["status"]) {
            array_push($where, ["status", "=", $all["status"]]);
        }
        if (isset($all["remark"]) && $all["remark"]) {
            array_push($where, ["remark", "LIKE", $all["remark"] . "%"]);
        }
        if (isset($all["start_time"]) && $all["start_time"] > 0 && isset($all["end_time"]) && $all["end_time"] > 0) {
            array_push($where, ["created_at", ">=", strtotime($all["start_time"])]);
            array_push($where, ["created_at", "<=", strtotime($all["end_time"])]);
        }
        $page = Request::param("page");
        $limit = Request::param("limit");
        $lists = WhiteIpModel::where($where)->limit(($page - 1) * $limit, $limit)->select();

        if (count($lists) > 0) {
            $lists = $lists->toArray();
            $admin_ids = array_column($lists, "admin_id");
            $admins = AdminModel::where("id", "in", $admin_ids)->column("name", "id");
            foreach ($lists as &$list) {
                //bugfix:处理异常undefined offset
                $index = $list["admin_id"];
                if (isset($admins[$index])) {
                    $list["admin_name"] = $admins[$index];
                    unset($list["admin_id"]);
                }
            }
            $count = WhiteIpModel::where($where)->count();
            return json([
                "code" => 0,
                "msg" => "",
                "data" => $lists,
                "count" => $count
            ]);
        }
        return self::bulidFail("未查询到相关数据");
    }

    public function add()
    {
        return view("add");
    }

    public function add_post()
    {
        $ip = Request::post("ip");
        $remark = Request::post("remark");
        $exists = WhiteIpModel::where("ip", "=", $ip)->find();
        if ($exists) return self::bulidFail("该IP已存在");
        $data = new WhiteIpModel();
        $data->ip = $ip;
        $data->remark = $remark;
        $data->status = 1;
        $data->admin_id = $this->userinfo->id;
        $ret = $data->save();
        if ($ret === false) {
            return self::bulidFail("添加失败");
        }

        // 记录日志
        $log_content = "添加ip白名单，ID：{$data->id}，IP：{$data->ip}，备注：{$data->remark}";
        parent::addAdminLog($log_content);

        return self::bulidSuccess("", "添加成功");
    }

    public function set_status()
    {
        $id = Request::post("id");
        $exists = WhiteIpModel::where("id", "=", $id)->find();
        if (!$exists) return self::bulidFail("该IP不存在");
        if ($exists->status == "生效" && $exists->ip == get_client_ip(0)) {
            return self::bulidFail("请勿禁止自己的IP！");
        }
        $exists->status = $exists->status == "生效" ? 0 : 1;

        $ret = $exists->save();
        if ($ret === false) {
            return self::bulidFail("设置失败");
        }

        // 记录日志
        $log_content = "设置ip白名单{$exists->status}，ID：{$exists->id}，IP：{$exists->ip}";
        parent::addAdminLog($log_content);

        return self::bulidSuccess("", "设置成功");
    }
}
